package com.suhui.task.service;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suhui.task.pojo.Member_Progress;
import com.suhui.task.pojo.Progress;
import com.suhui.task.pojo.progress.M;
import com.suhui.task.pojo.progress.P;
import com.suhui.task.pojo.progress.P_M;
import com.suhui.task.pojo.progress.P_all;
import org.springframework.stereotype.Service;

import java.lang.reflect.Member;
import java.util.Comparator;

@Service
public class My_util {

    /**
     * 进度信息页面
     * 数据格式转换
     */
    public JSONArray ChangeData(JSONArray data_array) {
        P p = new P();
        P_M p_m = new P_M();
        P_all p_all = new P_all();
        // 最终返回的 jsonArray
        JSONArray data = new JSONArray();
        for (Object json_pm: data_array) {
            p = JSONObject.parseObject(json_pm.toString(),P.class);
            JSONArray objects = JSON.parseArray(p.getProgress());
            for (Object json_p:objects) {
                p = JSONObject.parseObject(json_p.toString(),P.class);
                p_all.setId(p.getId());
                p_all.setBool_work(p.getBool_work());
                p_all.setBool_overwork(p.getBool_overwork());
                p_all.setGrasp(p.getGrasp());
                p_all.setProgress_content(p.getProgress_content());
                JSONArray p_m_array = JSON.parseArray(p.getMember_progress());
                for (Object json_p_m : p_m_array) {
                    p_m  = JSONObject.parseObject(json_p_m.toString(), P_M.class);
                    p_all.setYear_week(p_m.getYear_week());
                    p_all.setAsk_for_leave(p_m.getAsk_for_leave());
                    cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(p_all);
                    data.add(object);
                }
            }
        }
        return data;
    }

    /**
     * table页面
     * 数据格式转换
     * 姓名、组别搜索
     */
    public JSONArray ChangeData_n_g(JSONArray data_array) {
        P_all p_all = new P_all();
        P_M p_m = new P_M();
        P p = new P();
        M m = new M();
        // 最终返回的 jsonArray
        JSONArray data = new JSONArray();
        for (Object json_m: data_array) {
            m = JSONObject.parseObject(json_m.toString(),M.class);
            p_all.setName(m.getName());
            p_all.setGroup(m.getGroup());
            JSONArray object_m_p = JSON.parseArray(m.getMember_progress());
            for (Object json_p_m:object_m_p) {
                p_m = JSONObject.parseObject(json_p_m.toString(),P_M.class);
                p_all.setYear_week(p_m.getYear_week());
                p_all.setAsk_for_leave(p_m.getAsk_for_leave());
                p = JSONObject.parseObject(p_m.getProgress().toString(),P.class);
                p_all.setId(p.getId());
                p_all.setBool_work(p.getBool_work());
                p_all.setBool_overwork(p.getBool_overwork());
                p_all.setGrasp(p.getGrasp());
                cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(p_all);
                data.add(object);
            }
        }
        return data;
    }

    /**
     * table页面
     * 数据格式转换
     * 时间搜索
     */
    public JSONArray ChangeData_time(JSONArray data_array) {
        P_all p_all = new P_all();
        P_M p_m = new P_M();
        P p = new P();
        M m = new M();
        // 最终返回的 jsonArray
        JSONArray data = new JSONArray();
        for (Object json_m_p: data_array) {
            p_m = JSONObject.parseObject(json_m_p.toString(),P_M.class);
            p_all.setYear_week(p_m.getYear_week());
            p_all.setAsk_for_leave(p_m.getAsk_for_leave());
            p = JSONObject.parseObject(p_m.getProgress().toString(),P.class);
            p_all.setId(p.getId());
            p_all.setBool_work(p.getBool_work());
            p_all.setBool_overwork(p.getBool_overwork());
            p_all.setGrasp(p.getGrasp());
            JSONArray object_m = JSON.parseArray(p.getMember());
            for (Object json_m : object_m) {
                m  = JSONObject.parseObject(json_m.toString(), M.class);
                p_all.setName(m.getName());
                p_all.setGroup(m.getGroup());
                cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(p_all);
                data.add(object);
            }
        }
        return data;
    }

    /**
     * 自动生成 不重复id
     * 解决添加数据 id自增问题
     */
    public String getId() {
        // 返回不带-的字符串
        return IdUtil.simpleUUID();
    }

    /**
     * progress页面
     * 添加进度
     * 分类转换前端传递的数据
     */
    public Progress Data_to_progress(P p) {
        // 1.将p转换为 json格式字符串
        String string_p = JSON.toJSONString(p);
        // 2.将json格式字符串转换为java对象
        Progress progress = new Progress();
        Object parse = JSON.parse(string_p);
        progress = JSON.parseObject(parse.toString(), Progress.class);
        return progress;
    }
    public Member_Progress Data_to_Member_Progress(P p) {
        // 1.将p转换为 json格式字符串
        String string_p = JSON.toJSONString(p);
        // 2.将json格式字符串转换为java对象
        Member_Progress member_progress = new Member_Progress();
        Object parse = JSON.parse(string_p);
        member_progress = JSONObject.parseObject(parse.toString(),Member_Progress.class);
        return member_progress;
    }

    /**
     * 进度管理页面
     * 更新 成员表中 总的请假次数
     * 依据成员id 聚合成员每一周的请假次数 更新成员总的请假次数
     */
    public int count_ask_for_leave(JSONArray data_array) {
        int count_leave=0;
        for (Object json_m_p: data_array) {
            Member_Progress mp = JSONObject.parseObject(json_m_p.toString(),Member_Progress.class);
            count_leave += mp.getAsk_for_leave();
        }
        return count_leave;
    }

}
